package maquinavotar.view;

import java.awt.Button;
import java.awt.Label;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import maquinavotar.MaquinaVotarApp;
import maquinavotar.common.Candidate;
import maquinavotar.common.Election;
import maquinavotar.common.Post;
import maquinavotar.utils.RandomizingTool;

public class SelectionVoteScreen extends JPanel {
    
    final MaquinaVotarApp app;
    private boolean init = false;
    private ArrayList<JPanel> radioPanels = null;
    
    public SelectionVoteScreen(final MaquinaVotarApp app) {
    
        this.app = app;
        this.add(new Label("Error: no existen candidatos cargados."));
        
        Button btn_next = new Button("Retirar tarjeta");
        btn_next.addMouseListener(new java.awt.event.MouseAdapter() {
                @Override
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    app.showScreen(0,1);
                }
            });
        this.add(btn_next);
        
    }
    
    public void init() {
        
        if(app.cands != null) {

            this.removeAll();

            this.add(new Label("Realice su elección"));

            if(app.vote == null) {
                app.vote = new ArrayList<Election>();
                app.cands = RandomizingTool.getArrayRandom(app.cands);
            }

            showCandidates();

            Button btn_next = new Button("Confirmar");
            btn_next.addMouseListener(new java.awt.event.MouseAdapter() {
                    @Override
                    public void mouseClicked(java.awt.event.MouseEvent evt) {
                        saveVoto();
                        app.showScreen(2,1);
                    }
                });
            this.add(btn_next);

            Button btn_reset = new Button("Resetear");
            btn_reset.addMouseListener(new java.awt.event.MouseAdapter() {
                    @Override
                    public void mouseClicked(java.awt.event.MouseEvent evt) {
                        init();
                        app.showScreen(1, 1);
                    }
                });
            this.add(btn_reset);
            
            init = true;
        
        }
    }

    public boolean isInitialization() {
        return init;
    }
    
    private void showCandidates() {
        
        radioPanels = new ArrayList<JPanel>();
        
        for(String post : Post.getAllPost()) {
            ButtonGroup bgroup = new ButtonGroup();
            JPanel radioPanel = new JPanel();
            radioPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), post));
            radioPanel.setName(post);

            for(Candidate c : app.cands) {
                JRadioButton radioButton = new JRadioButton(c.nombre, false);
                bgroup.add(radioButton);
                radioPanel.add(radioButton);
            }

            this.add(radioPanel);
            radioPanels.add(radioPanel);
        }
        
    }
    
    private void saveVoto() {
        app.vote = new ArrayList<Election>();
        for(int i=0; i<radioPanels.size(); i++) {
            JPanel radioPanel = radioPanels.get(i);
            for (int j = 0; j < radioPanel.getComponentCount(); j++) {
                JRadioButton radioButton = (JRadioButton) radioPanel.getComponents()[j];
                if(radioButton.isSelected()) {
                    app.vote.add(new Election(radioButton.getText(), radioPanel.getName()));
                    break;
                }
            }
        }
    }
    
}